﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
namespace LdExpress
{
    public class EventInfo
    {

        public long ID = -1;
        public string Name = "";
        public string Description = "";
        public DateTime StartDate = DateTime.Now;
        public DateTime EndDate = DateTime.Now;
        public long OwnerID = -1;
        public long CustID = -1;
        public long UserID = -1;
        public int EventType = 0;
        public int AllDay = 0;
        public string ColorEV = "";
        public int AlarmTime = 1;
        public int AlarmType = 0;
        public int Status = 1;
        public int TimeUnit = -1;//1: Dau gio sang;2: Cuoi gio sang; 3: dau gio chieu; 4: cuoi gio chieu;5: dau tuan;
        //6:giua tuan;7: cuoi tuan;8: dau thang;9:giua thang;10:cuoi thang;11:dau nam;12:giua nam;13:cuoi nam
        public DateTime LastUpdatedDate = DateTime.Now;
        public string FilePath = "";
        public EventInfo()
        {
        }

        public EventInfo(long lID)
        {
            this.ID = lID;
        }

        public void Save(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            string strTableName = "tbl_eventinfo";

            if (ID <= 0) // new object, we insert new record to database
            {
                strSQL = "Insert into " + strTableName + " (" +
                            "`CustID`" +
                            ",`UserID`" +
                            ",`Name`" +
                            ",`Description`" +
                            ",`StartDate`" +
                            ",`EndDate`" +
                            ",`EventType`" +
                            ",`AllDay`" +
                            ",`AlarmTime`" +
                            ",`AlarmType`" +
                            ",`ColorEV`" +
                            ",`OwnerID`" +
                            ",`Status`" +
                            ",`TimeUnit`" +
                            ",`LastUpdatedDate`" +
                            ",`FilePath`" +
                            ") " +
                         " values(" +
                             CustID.ToString() +
                             "," + UserID.ToString() +
                             ",'" + DBModule.RefineString(Name.ToString()) + "'" +
                             "," + "'" + DBModule.RefineString(Description.ToString()) + "'" +
                             ",'" + StartDate.ToString("yyyy-MM-dd HH:mm:ss") + "'" +
                             ",'" + EndDate.ToString("yyyy-MM-dd HH:mm:ss") + "'" +
                             "," + EventType.ToString() +
                             "," + AllDay.ToString() +
                             "," + AlarmTime.ToString() +
                             "," + AlarmType.ToString() +
                             ",'" + DBModule.RefineString(ColorEV) + "'" +
                             "," + OwnerID.ToString() +
                             "," + Status.ToString() +
                             "," + TimeUnit.ToString() +
                             ",'" + LastUpdatedDate.ToString("yyyy-MM-dd HH:mm:ss") + "'" +
                             "," + "'" + DBModule.RefineString(FilePath.ToString()) + "'" +
                             ")";
                // run SQL statement
                //throw new Exception(strSQL);
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("reminder", "add", cn, trans);
            }
            else // edit object
            {
                // build SQL statement
                strSQL = "Update " + strTableName + " set " +
                            "CustID=" + CustID.ToString() +
                            ",UserID=" + UserID.ToString() +
                            ",Name='" + DBModule.RefineString(Name.ToString()) + "'" +
                            ",Description='" + DBModule.RefineString(Description.ToString()) + "'" +
                            ",StartDate='" + StartDate.ToString("yyyy-MM-dd HH:mm:ss") + "'" +
                            ",EndDate='" + EndDate.ToString("yyyy-MM-dd HH:mm:ss") + "'" +
                            ",EventType = " + EventType.ToString() +
                            ",AllDay = " + AllDay.ToString() +
                            ",AlarmTime = " + AlarmTime.ToString() +
                            ",AlarmType = " + AlarmType.ToString() +
                            ",ColorEV='" + DBModule.RefineString(ColorEV) + "'" +
                            ",OwnerID = " + OwnerID.ToString() +
                            ",Status = " + Status.ToString() +
                            ",TimeUnit = " + TimeUnit.ToString() +
                            ",LastUpdatedDate='" + LastUpdatedDate.ToString("yyyy-MM-dd HH:mm:ss") + "'" +
                            ",FilePath='" + DBModule.RefineString(FilePath.ToString()) + "'" +
                    " where ID=" + ID.ToString();
                // run SQL statement
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("reminder", "edit", cn, trans);
            }
        }
        public void Delete(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "DELETE FROM tbl_eventinfo WHERE ID = " + this.ID.ToString();
            DBModule.ExecuteNonQuery(strSQL, cn, trans);
            UserObjectsLog.SaveObjectActionLog("reminder", "delete", cn, trans);
        }
        public void Archive(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "update tbl_eventinfo set Status=-1 WHERE ID = " + this.ID.ToString();
            DBModule.ExecuteNonQuery(strSQL, cn, trans);
            UserObjectsLog.SaveObjectActionLog("reminder", "delete", cn, trans);
        }
        public void Delete(long CustID, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "DELETE FROM tbl_eventinfo WHERE CustID = " + CustID;
            DBModule.ExecuteNonQuery(strSQL, cn, trans);
            UserObjectsLog.SaveObjectActionLog("reminder", "delete", cn, trans);
        }
        public void Load(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "SELECT * FROM tbl_eventinfo WHERE ID = " + ID.ToString() + " limit 0,1";
            DataSet ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID")) ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("CustID")) CustID = long.Parse(dr["CustID"].ToString());
                if (!dr.IsNull("UserID")) UserID = long.Parse(dr["UserID"].ToString());
                if (!dr.IsNull("Name")) Name = dr["Name"].ToString();
                if (!dr.IsNull("Description")) Description = dr["Description"].ToString();
                if (!dr.IsNull("StartDate")) StartDate = DateTime.Parse(dr["StartDate"].ToString());
                if (!dr.IsNull("EndDate")) EndDate = DateTime.Parse(dr["EndDate"].ToString());
                if (!dr.IsNull("OwnerID")) OwnerID = long.Parse(dr["OwnerID"].ToString());
                if (!dr.IsNull("EventType")) EventType = int.Parse(dr["EventType"].ToString());
                if (!dr.IsNull("AllDay")) AllDay = int.Parse(dr["AllDay"].ToString());
                if (!dr.IsNull("ColorEV")) ColorEV = dr["ColorEV"].ToString();
                if (!dr.IsNull("AlarmType")) AlarmType = int.Parse(dr["AlarmType"].ToString());
                if (!dr.IsNull("AlarmTime")) AlarmTime = int.Parse(dr["AlarmTime"].ToString());
                if (!dr.IsNull("Status")) Status = int.Parse(dr["Status"].ToString());
                if (!dr.IsNull("TimeUnit")) TimeUnit = int.Parse(dr["TimeUnit"].ToString());
                if (!dr.IsNull("LastUpdatedDate")) LastUpdatedDate = DateTime.Parse(dr["LastUpdatedDate"].ToString());
                if (!dr.IsNull("FilePath")) FilePath = dr["FilePath"].ToString();
            }
        }

        public void GetList(string strCondition, string strEventType, string CustomerIDs, string OrderBy, int NumOfItemsPerPage, int PageNo, out int NumberOfPages, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            NumberOfPages = 0;
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select * from tbl_eventinfo where 1=1 ";

            strSQL = "Select tbl_eventinfo.*,tbl_adminusers.UserName from tbl_eventinfo inner join tbl_adminusers on tbl_eventinfo.UserID=tbl_adminusers.id where 1=1 ";

            if ((strCondition != null) && (strCondition != ""))
            {
                strSQL += strCondition;
            }
            if ((CustomerIDs != null) && (CustomerIDs != ""))
            {
                strSQL += " And UserID=" + CustomerIDs;
            }
            if ((OrderBy != null) && (OrderBy != ""))
                strSQL += " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            if (NumOfItemsPerPage > 0)
            {
                ds = DBModule.ExecuteQuery(strSQL, NumOfItemsPerPage, PageNo, out NumberOfPages, cn, trans);
            }
            else
                ds = DBModule.ExecuteQuery(strSQL, cn, trans);

        }

        public void DoSearch(string strKeywords, string OrderBy, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select `ID` from tbl_eventinfo where 1=1 ";

            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            ds = DBModule.ExecuteQuery(strSQL, cn, trans);
        }

    }
}

